import 'package:flutter/material.dart';
import 'package:acgn_pioneer/api/api_code.dart';
import 'package:acgn_pioneer/event/model_event.dart';
import 'package:acgn_pioneer/global/_index.dart';
// import 'package:fluttertoast/fluttertoast.dart';

import '../app.dart';
import '../public.dart';

/// 全局http错误事件
mixin HttpErrorMixin on State<MyApp> {
  var subscription;

  @override
  void initState() {
    super.initState();

    subscription = ACGN.event.on<HttpErrorEvent>().listen((event) {
      errorHandleFunction(event.code, event.message);
    });
  }

  @override
  void dispose() {
    super.dispose();

    // 注销掉事件
    subscription.cancel();
  }

  ///网络错误提醒
  errorHandleFunction(int code, message) {
    switch (code) {
      case ApiCode.NETWORK_ERROR:
        // ACGN.toast.message(I18n.of(ACGN.navGK.currentContext).Title);
        break;
      case ApiCode.NETWORK_TIMEOUT:
        //超时
        // showToast(I18n.of(_context).network_error_timeout);
        break;
      case 401:
        // showToast(I18n.of(_context).network_error_401);
        break;
      case 403:
        // showToast(I18n.of(_context).network_error_403);
        break;
      case 404:
        // showToast(I18n.of(_context).network_error_404);
        break;
      case 422:
        // showToast(I18n.of(_context).network_error_422);
        break;
      default:
        // showToast(I18n.of(_context).network_error_unknown + " " + message);
        break;
    }
  }

  showToast(String message) {
    // Fluttertoast.showToast(
    //     msg: message,
    //     gravity: ToastGravity.CENTER,
    //     toastLength: Toast.LENGTH_LONG);
  }
}
